package com.study.rabbitmq.advance;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.MessageProperties;

/**
 * 发送者：
 * 可靠性
 */
public class Send {

	private final static String QUEUE_NAME = "hello";
	private final static String EXCHANGE_NAME = "";

	public static void main(String[] argv) throws Exception {
		ConnectionFactory factory = new ConnectionFactory();
		factory.setHost("192.168.20.68");
		factory.setPort(5672);
		factory.setUsername("leke");
		factory.setPassword("leke@@@");
		Connection connection = factory.newConnection();
		Channel channel = connection.createChannel();

		channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//		channel.queueBind(, , );
//		channel.exchangeBind(, , );


		String message = "Hello World!";

		/**
		 * 普通
		 */
		int i = 0;
		while (true) {
			Thread.sleep(1000);
			i++;
			channel.basicPublish(EXCHANGE_NAME, QUEUE_NAME, null, (""+i).getBytes("UTF-8"));
			System.out.println("publish message: "+i);
		}

//        /**
//         * 发送事务机制
//         */
//        try {
//            //将当前Channel设置成是transaction模式
//            channel.txSelect();
//            channel.basicPublish(EXCHANGE_NAME, QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, "transaction messages".getBytes());
//            channel.txCommit();
//        } catch (Exception e) {
//            channel.txRollback();
//        }
//        /**
//         * 发送confirm机制
//         */
//        try {
//            channel.confirmSelect();
//            for (int i = 0; i < 20; i++) {
//                channel.basicPublish(EXCHANGE_NAME, QUEUE_NAME, MessageProperties.PERSISTENT_TEXT_PLAIN, "transaction messages".getBytes());
//                //阻塞 方法 同步确认
//                if (!channel.waitForConfirms()) {
//                    System.out.println("send message failed");
//                    // 重发 或者其他操作
//                }
//            }
//
//
//            //异步监听
//            //channel.addConfirmListener();
//
//
//        } catch (Exception e) {
//            channel.txRollback();
//        }

//		channel.close();
//		connection.close();
	}
}
